<?php
  require_once("connect.php") ;
  require_once("logger.php") ;
  
  
	session_start();
	$log = new LOGGER();
	if (isset($_POST['submit_logout'])) {
     $log->log('Deconnexion de '. $_SESSION['LOGIN']);
	   session_unset();
	   session_destroy();
   	 session_start();
  }
	if (isset($_POST['login']) && isset($_POST['submit_login'])) {
  		$login = (isset($_POST['login'])) ? $_POST['login'] : '';
  		$pass  = (isset($_POST['pass']))  ? $_POST['pass']  : '';
  		
  		$sql = SQL::getConnection();

      $req = 'SELECT ID_USER,LOGIN,PASS,ADMIN,LISTE_DROIT  FROM pmg_user WHERE LOGIN = \''.$login.'\'';

      $result = $sql->query($req);
      $array_result = $result->fetch_assoc();

     
     
		  if (($login == $array_result['LOGIN']) && (password_verify ( $pass  ,$array_result['PASS'] ) ) ) {
			   $_SESSION['ID_USER'] = $array_result['ID_USER'];
         $_SESSION['LOGIN'] = $array_result['LOGIN'];
			   $_SESSION['ADMIN'] = $array_result['ADMIN'];
         $_SESSION['LISTE_DROIT'] = $array_result['LISTE_DROIT'];
         
         $log->log('Connexion de '. $_SESSION['LOGIN']);
  		}
  		else
  		{
      	
        $log->log('Tentative de connexion de '. $login);
  			// une erreur de saisie ...?
  			echo '<p style="color:#FF0000; font-weight:bold;">Erreur de connexion.</p>';
  		}
      $result->close();
  }
  
	if(isset($_SESSION['LOGIN'])) {

		echo '<form id="idConn" method="post" action="">'."\n";
		echo '  <p><label>'.$_SESSION['LOGIN'].':</label>';
		echo '  <input type="submit" id="submit" name="submit_logout" value="Deconnexion" /></p>';
		echo '</form>'."\n";
    ?>
    <script>
      function doSessionInit(){
          dhxMainLayout.cells("a").attachObject("idConn");
      }
    </script>
    
    <?php
    
	} else {
	  include("head-html.php"); 
    ?>
    <div id="idLogin"  style="width:1000px; height:800px;">
	  <br><br><div class="formArea"><table style="text-align: left; margin-left: auto; margin-right: auto;" cellspacing="0" cellpadding="0" >
    <tr><td id="loginTitle">Se connecter à la gestion des compétitions<br>F.S.C.F.</td></tr><tr><td id="loginForm">
    <p>Entrez le nom de login et le mot de passe dans les champs "Login" et "Mot de passe"<br>respectivement, puis cliquez sur "Login".</p>
		<form id="conn" method="post" action="" onsubmit="setValue(\'pass\',hex_md5(getValue(\'pass\')))">
	  <table class="formFields" cellspacing="0" width="100%">
		<tr><td><label for="login">Login :</label></td><td><input type="text" id="login" name="login" /></td></tr>
		<tr><td><label for="pass">Mot de Passe :</label></td><td><input type="password" id="pass" name="pass" /></td></tr>
		<tr><td><input type="submit" id="submit" name="submit_login" value="Login" /></td>
		</table></form></td></tr></table></div>
    </div>
    <script>
      function doSessionInit(){
         dhxMainLayout.cells("c").attachObject("idLogin");

         
         }
    </script>

    <?php
	  include("foot-html.php"); 
		
		die();
  }
  
  
?>
